package com.moonshineproject
{
	import com.moonshineproject.commands.CloseTabCommand;
	import com.moonshineproject.commands.ICommand;
	import com.moonshineproject.commands.NewFileCommand;
	import com.moonshineproject.commands.OpenFileCommand;
	import com.moonshineproject.commands.QuitCommand;
	import com.moonshineproject.commands.SaveAsCommand;
	import com.moonshineproject.commands.SaveFileCommand;
	import com.moonshineproject.events.CloseTabEvent;
	import com.moonshineproject.events.OpenFileEvent;
	import com.moonshineproject.plugin.menu.MenuPlugin;
	
	import flash.events.Event;
	
	public class IDEController
	{
		protected var commands:Object = {};

		public function IDEController() {
			init();
		}

		public function init():void {
			
			setupBindings();
			setupListener();
			
			// Make sure these nasty commands gets compiled in.
			var opfc:OpenFileCommand;
			var sfc:SaveFileCommand;
		}
		
		public function setupBindings():void {
			
			
			commands[MenuPlugin.MENU_OPEN_EVENT] = 	OpenFileCommand;
			commands[OpenFileEvent.OPEN_FILE] = 		OpenFileCommand;
			commands[MenuPlugin.MENU_SAVE_EVENT] = 	SaveFileCommand; 
			commands[MenuPlugin.MENU_SAVE_AS_EVENT] = 	SaveAsCommand;
			commands[MenuPlugin.MENU_QUIT_EVENT] = 	QuitCommand;
			commands[CloseTabEvent.EVENT_CLOSE_FILE] = 	CloseTabCommand;
			commands[MenuPlugin.MENU_NEW_FILE_EVENT] =	NewFileCommand;
		}
		
		public function setupListener():void {
			var ged:GlobalEventDispatcher = GlobalEventDispatcher.getInstance();
			for (var eventName:String in commands) {
				ged.addEventListener(eventName, execCommand);
			}
		}
		
		public function execCommand(event:Event):void {
			var cmd:ICommand = new commands[event.type]();
			cmd.execute(event);
		}
		
	}
}